import caffe
import tempfile
import numpy as np

def create_net():
    f = tempfile.NamedTemporaryFile(mode='w+', delete=False)
    f.write(
        """
        name: 'pair_nn'
    
        #level base
        layer { name: "data" type: "DummyData" top: "lv" dummy_data_param { shape { dim: 2048 dim: 1 dim: 12 dim: 202 } } }
        layer { name: 'conv' type: 'Convolution' bottom: "lv" top: "conv" convolution_param { engine: CAFFE num_output: 1 kernel_w: 1 kernel_h: 5  weight_filler {type: "xavier"} } }
        layer { name: "relu" type: "ReLU" bottom: "conv" top: "conv" }
        layer { name: "pool" type: "Pooling" bottom: "conv" top: "pool" pooling_param { pool: AVE kernel_h: 3 kernel_w: 1 stride_h: 1 stride_w: 1} }
        #level upper
        layer { name: "data_upper" type: "DummyData" top: "lv_upper" dummy_data_param { shape { dim: 2048 dim: 1 dim: 12 dim: 202 } } }
        layer { name: 'conv_upper' type: 'Convolution' bottom: "lv_upper" top: "conv_upper" convolution_param { engine: CAFFE num_output: 1 kernel_w: 1 kernel_h: 5  weight_filler {type: "xavier"} } }
        layer { name: "relu_upper" type: "ReLU" bottom: "conv_upper" top: "conv_upper" }
        layer { name: "pool_upper" type: "Pooling" bottom: "conv_upper" top: "pool_upper" pooling_param { pool: AVE kernel_h: 3  kernel_w: 1 stride_h: 1 stride_w: 1} }
    
        layer { name: "concat" type: "Concat" bottom: "pool" bottom: "pool_upper" top: "ip1" concat_param { axis: 1 } }
    
        #proposal loss
        layer { name: "label" type: "DummyData" top: "label" dummy_data_param { shape { dim: 2048 dim: 1 dim: 1 dim: 1 } } }
        layer { name: "ip1" type: "InnerProduct" bottom: "ip1" top: "ip2" inner_product_param { num_output: 500 weight_filler {type: "xavier"} } }
        layer { name: "ip2" type: "InnerProduct" bottom: "ip2" top: "rs" inner_product_param { num_output: 2 weight_filler {type: "xavier"} } }
        layer { name: "loss" type: "SoftmaxWithLoss" bottom: "rs" bottom: "label" top: "loss" loss_weight: 1 loss_param {ignore_label: -1 normalize: true} }
        """)
    f.close()
    return f.name

def create_deploy():
    f = tempfile.NamedTemporaryFile(mode='w+', delete=False)
    f.write(
        """
        name: 'pair_nn'
    
        #level base
        layer { name: "data" type: "DummyData" top: "lv" dummy_data_param { shape { dim: 1 dim: 1 dim: 12 dim: 202 } } }
        layer { name: 'conv' type: 'Convolution' bottom: "lv" top: "conv" convolution_param { engine: CAFFE num_output: 1 kernel_w: 1 kernel_h: 5  weight_filler {type: "xavier"} } }
        layer { name: "relu" type: "ReLU" bottom: "conv" top: "conv" }
        layer { name: "pool" type: "Pooling" bottom: "conv" top: "pool" pooling_param { pool: AVE kernel_h: 3 kernel_w: 1 stride_h: 1 stride_w: 1} }
        # #level upper
        # layer { name: "data_upper" type: "DummyData" top: "lv_upper" dummy_data_param { shape { dim: 1 dim: 1 dim: 12 dim: 202 } } }
        # layer { name: 'conv_upper' type: 'Convolution' bottom: "lv_upper" top: "conv_upper" convolution_param { engine: CAFFE num_output: 1 kernel_w: 1 kernel_h: 5  weight_filler {type: "xavier"} } }
        # layer { name: "relu_upper" type: "ReLU" bottom: "conv_upper" top: "conv_upper" }
        # layer { name: "pool_upper" type: "Pooling" bottom: "conv_upper" top: "pool_upper" pooling_param { pool: AVE kernel_h: 3  kernel_w: 1 stride_h: 1 stride_w: 1} }
        # 
        # layer { name: "concat" type: "Concat" bottom: "pool" bottom: "pool_upper" top: "ip1" concat_param { axis: 1 } }
        # 
        #proposal
        # layer { name: "ip1" type: "InnerProduct" bottom: "ip1" top: "ip2" inner_product_param { num_output: 500 } }
        # layer { name: "ip2" type: "InnerProduct" bottom: "ip2" top: "rs" inner_product_param { num_output: 2 } }
        # layer { name: "loss" type: "Softmax" bottom: "rs" top: "loss"}
        """)
    f.close()
    return f.name

if __name__=='__main__':
    df = create_deploy()
    Net = caffe.Net(df,caffe.TEST)

    Net.blobs['lv'] = np.random.randn(1,1,12,202)
    Net.blobs['lv_upper'] = np.random.randn(1,1,12,202)

    out = Net.forward()

    out.keys()

    out['conv'].shape

    out['pool'].shape

    out['ip2'].shape